package com.controller;

import com.alibaba.fastjson.JSON;
import com.pojo.DishType;
import com.pojo.Msg;
import com.pojo.Order;
import com.service.DishTypeService;
import com.service.StatisticsService;
import com.shiro.controller.BaseController;
import com.utils.DateUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

//@RequiresPermissions(value={"statistics:*"})
@Controller
@ResponseBody
public class StatisticsController extends BaseController {
    @Autowired
    private StatisticsService statisticsService;  //统计服务

    @Autowired
    private DishTypeService dishTypeService;  //食品类型的服务

    @RequestMapping(value = "statistic/dishtype",method = RequestMethod.GET)
    public Msg statDishType(){
        List<DishType> dishTypeList = dishTypeService.getDishType();
        Map<String, Object> map = new HashMap<>();
        for(DishType dishType: dishTypeList){
            Integer sumCount = statisticsService.getFoodTypeStat(dishType.getDtId());
            map.put(dishType.getDtName(), sumCount);
        }
        return Msg.success().add("dishtypeStat", map);
    }


    @RequestMapping(value = "statistic/dailyTotal", method = RequestMethod.GET)
    public Msg statRecentTotal(){ //最近7天的营业额显示
        List<String> dateList = statisticsService.getCurrentWeekDays();
        Map<String, Object> map = new TreeMap<>(); //有序，按key排序



        for(String date: dateList){
            BigDecimal totalMoney = statisticsService.getDailyTotalStat(date);
            map.put(date, totalMoney);
        }
        return Msg.success().add("dailyTotalStat", map);
    }



    //复杂图标传值
    //时间区间 +  类型
    @RequestMapping(value = "statistic/complex", method = RequestMethod.POST)
    public Msg statComplex(@RequestBody Map<String, List> map){


        for (String key : map.keySet()) {
            System.out.println(key);

            System.out.println(JSON.toJSONString(map));
//            List<String> a = map.get("dateRange");
//            for (String date : a) {
//                String newDate = DateUtil.parseTime(date);
//                System.out.println(newDate);
//            }


        }

//        String date1 = DateUtil.parseTime(map.get("dateRange").get(0));
//         map.get("dishTypeSelect");
//        List<String> dateList = map.get("dateRange");
//        List<String> dishtypeList = map.get("dishTypeSelect");
        List<String> newDate = statisticsService.getCleanDateList(map.get("dateRange"));

        Integer c = Integer.parseInt((String)map.get("dishTypeSelect").get(0));



        //获取指定类型的数据,两个方法有冗余但是为节约时间不再修改
        List<Order> partOrderList = statisticsService.selectOrderByDateAndDishType(c, newDate);

        List<Order> allOrderList = statisticsService.selectOrderByDateRange(newDate);


        //存储指定类型的营业额
        Map<String, BigDecimal> map1 = (Map<String, BigDecimal>) statisticsService.getDateRangeTotalMoney(partOrderList);

        //指定日期范围的营业额
        Map<String, BigDecimal> map2 = (Map<String, BigDecimal>) statisticsService.getDateRangeTotalMoney(allOrderList);


        Msg msg = Msg.success().add("allOrderList", map2);
        msg.add("partOrderList", map1);

        return  msg;
    }



    //修改过后的代码
    @RequestMapping(value = "statistic/complex2", method = RequestMethod.POST)
    @ResponseBody
    public Msg statComplex2(@RequestBody Map<String, List> map){
        //得到干净的列表
        List<String> newDate = statisticsService.getCleanDateList(map.get("dateRange"));



        //统计近段时间的营业额
        Map<String, Object> mapAll = new TreeMap<>();
        List<Date> dateList = statisticsService.findDates(DateUtil.parseDateTime(newDate.get(0)), DateUtil.parseDateTime(newDate.get(1)));
        for(Date date: dateList){
            String dateStr = DateUtil.parseDateToString(date, DateUtil.fm1);
            BigDecimal totalMoney = statisticsService.getDailyTotalStat(dateStr);
            mapAll.put(dateStr, totalMoney);
        }
        Msg msg = Msg.success().add("dateRangeTotal", mapAll);



        //统计近段时间的商品类型的销售数量
        Map<String, Object> mapDishtype = new TreeMap<>();

        List<DishType> dishTypeList = dishTypeService.getDishType(); //获取全部商品类型的服务

        for(DishType dishType: dishTypeList){
            Integer sumCount = statisticsService.getFoodTypeStat(dishType.getDtId());
            mapDishtype.put(dishType.getDtName(), sumCount);
        }
        msg.add("dishtypeStat", mapDishtype);


        //统计近段时间商品类型的销售金额
        Map<String, Object> mapDishTypeTotal = new TreeMap<>();
        List<String> newDate2 = new ArrayList<>();

        for(String subDate: newDate){
            String newSubDate = DateUtil.parseDateToString(DateUtil.parseDateTime(subDate),DateUtil.fm1);
            newDate2.add(newSubDate);
        }


        for(DishType dishType: dishTypeList){
            List<Order> orderList = statisticsService.selectOrderByDateAndDishType(dishType.getDtId(), newDate2);
            BigDecimal subTotal = statisticsService.getDateRangeTotalByDishTyepId(orderList);
            mapDishTypeTotal.put(dishType.getDtName(), subTotal);
        }

        msg.add("dishtypeSubTotal", mapDishTypeTotal);


        return msg;

    }
}
